//
//  RBGoodsDetailFootView.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/5/16.
//  Copyright © 2018年 Tzyang. All rights reserved.
//  商品详情footer View


#import "RBGoodsDetailFootView.h"
/**自定义button 上面图片 下面文字*/
@interface RBGoodDetailFooterButton:UIButton;
+ (RBGoodDetailFooterButton *)rbGoodDetailFooterButtonWithImg:(NSString *)img  action:(SEL)action target:(id)target titleString:(NSString *)titleStr;
@end
@implementation RBGoodDetailFooterButton
+ (RBGoodDetailFooterButton *)rbGoodDetailFooterButtonWithImg:(NSString *)img  action:(SEL)action target:(id)target titleString:(NSString *)titleStr{
    RBGoodDetailFooterButton *btn = [RBGoodDetailFooterButton buttonWithType:(UIButtonTypeCustom)];
    
    [btn setImage:[UIImage imageNamed:img] forState:(UIControlStateNormal)];
    [btn setTitle:titleStr forState:(UIControlStateNormal)];
    [btn setTitleColor:XJCOLOR_RGB(158, 165, 168) forState:(UIControlStateNormal)];
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [btn.titleLabel setFont:[UIFont systemFontOfSize:13.0]];
    
    //target 肯定不能是自己
    [btn addTarget:target action:action forControlEvents:(UIControlEventTouchUpInside)];
    
    return btn;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat h = self.bounds.size.height;

    self.imageView.rb_y = h*0.1;
    self.imageView.rb_x = (self.bounds.size.width - self.imageView.rb_width)/2;
    
    self.titleLabel.rb_x = 0;
    self.titleLabel.rb_width = self.rb_width;
    self.titleLabel.rb_y = h * 0.6;
}

@end

@interface RBGoodsDetailFootView()
/**店铺*/
@property(nonatomic, strong) RBGoodDetailFooterButton *storeButton;
/**客服*/
@property(nonatomic, strong) RBGoodDetailFooterButton *customServiceButton;
/**收藏*/
@property(nonatomic, strong) RBGoodDetailFooterButton *starButton;
/**加入购物车*/
@property(nonatomic, strong) UIButton *addToShopcartButton;
/**立即购买*/
@property(nonatomic, strong) UIButton *buyNowButton;
@end

@implementation RBGoodsDetailFootView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    [self addSubview:self.storeButton];
    [self addSubview:self.customServiceButton];
    [self addSubview:self.starButton];
    [self addSubview:self.addToShopcartButton];
    [self addSubview:self.buyNowButton];
}

#pragma mark - setter

- (RBGoodDetailFooterButton *)storeButton {
    if (!_storeButton) {
        _storeButton = [RBGoodDetailFooterButton rbGoodDetailFooterButtonWithImg:@"doods_shop" action:@selector(clickStoreAction) target:self titleString:@"店铺"];
    }
    return _storeButton;
}

- (RBGoodDetailFooterButton *)customServiceButton {
    if (!_customServiceButton) {
        _customServiceButton = [RBGoodDetailFooterButton rbGoodDetailFooterButtonWithImg:@"goods_kefu" action:@selector(clickCustomServiceAction) target:self titleString:@"客服"];
    }
    return _customServiceButton;
}

- (RBGoodDetailFooterButton *)starButton {
    if (!_starButton) {
        _starButton = [RBGoodDetailFooterButton rbGoodDetailFooterButtonWithImg:@"goods_shoucang_off" action:@selector(clickStarAction:) target:self titleString:@"收藏"];
        
        // 设置选中状态图片
        [_starButton setImage:[UIImage imageNamed:@"goods_shoucang_on"] forState:(UIControlStateSelected)];
    }
    return _starButton;
}



- (UIButton *)rb_createBtnWithAction:(SEL)action title:(NSString *)title{
    UIButton *btn =  [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    [btn addTarget:self action:action forControlEvents:(UIControlEventTouchUpInside)];
    [btn setTitle:title forState:(UIControlStateNormal)];
    [btn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [btn setTitleShadowColor:[UIColor lightGrayColor] forState:(UIControlStateHighlighted)];
    
    btn.titleLabel.font = [UIFont systemFontOfSize:16.0]; // 5s适配问题
    
    btn.clipsToBounds = YES;
    btn.layer.cornerRadius = 13;
    
    return btn;
}

- (UIButton *)addToShopcartButton {
    if (!_addToShopcartButton) {
        _addToShopcartButton = [self rb_createBtnWithAction:@selector(clictAddToShopcartAction) title:@"加入购物车"];
        
        _addToShopcartButton.layer.borderColor = [UIColor blackColor].CGColor;
        _addToShopcartButton.layer.borderWidth = 1.0;
    }
    return _addToShopcartButton;
}

- (UIButton *)buyNowButton {
    if (!_buyNowButton) {
        _buyNowButton = [self rb_createBtnWithAction:@selector(clickBuyNowAction) title:@"立即购买"];
        _buyNowButton.backgroundColor = XJCOLOR_RGB(255, 218, 67); // 黄色
    }
    return _buyNowButton;
}
#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat h = self.bounds.size.height;
    CGFloat w = self.bounds.size.width;
    CGFloat btn_w = ADAPTWidth(40);
    CGFloat kMargin = ADAPTWidth(5); //间距
    // 加入购物车、立即购买两个按钮的宽度
    CGFloat buyNowBtn_w = (w - kMargin*9 - btn_w*3) / 2;
    
    [self.storeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(kMargin);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(btn_w, h));
    }];
    
    [self.customServiceButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.storeButton.mas_right).offset(kMargin);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(btn_w, h));
    }];
    
    [self.starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.customServiceButton.mas_right).offset(kMargin);
        make.centerY.equalTo(self);
        make.size.mas_equalTo(CGSizeMake(btn_w, h));
    }];
    
    //
    [self.addToShopcartButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.starButton.mas_right).offset(kMargin);
        make.size.mas_equalTo(CGSizeMake(buyNowBtn_w, h*0.8));
    }];
    
    [self.buyNowButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.left.equalTo(self.addToShopcartButton.mas_right).offset(kMargin*2);
        make.size.mas_equalTo(CGSizeMake(buyNowBtn_w, h*0.8));
    }];
}

#pragma mark - 点击事件
- (void)clickStoreAction{
    if (self.clickStoreBlock) {
        self.clickStoreBlock();
    }
//    XJLog(@"店铺");
}
- (void)clickCustomServiceAction {
//    XJLog(@"客服");
    if (self.clickKeFuBlock) {
        self.clickKeFuBlock();
    }
}


- (void)clickStarAction:(RBGoodDetailFooterButton *)btn {
    NSLog(@"选中状态:%d",btn.selected);
    if (self.clickStarBlock) {
        self.clickStarBlock(btn.selected);
    }
    btn.selected  = !btn.selected;
}

- (void)clictAddToShopcartAction {
//    XJLog(@"加入购物车");
    if (self.clickAddShopcartBlock) {
        self.clickAddShopcartBlock();
    }
}
- (void)clickBuyNowAction{
//    XJLog(@"立即购买");
    if (self.clickBuynowBlock) {
        self.clickBuynowBlock();
    }
}

#pragma mark - setter
- (void)setIsStar:(BOOL)isStar {
    _isStar = isStar;
    self.starButton.selected = isStar;
}
@end
